package com.elitel.frame.base.controller.accessControl;

import com.elitel.frame.base.service.ApplicationCodeService;
import com.elitel.frame.base.service.SysReqLogService;
import com.elitel.frame.main.entity.AcApp;
import com.elitel.frame.main.entity.SysReqLog;
import com.elitel.frame.main.entity.vo.JsonResult;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 访问控制-应用编码控制器
 *
 * @author hi_bj
 */
@Controller
@RequestMapping("/accesscontrol/appcode")
public class ApplicationCodeController {

    @Autowired
    ApplicationCodeService applicationCodeService;

    @Autowired
    SysReqLogService sysReqLogService;

    /**
     * 获取数据分页
     *
     * @param pageInfo
     * @param acApp
     * @return JsonResult
     */
    @RequestMapping("/getDataPage")
    @ResponseBody
    public Object getDataPage(PageInfo pageInfo, AcApp acApp) {
        JsonResult jr = new JsonResult();
        PageResponseVo pageResponseVo = new PageResponseVo();
        PageInfo dataPage = applicationCodeService.getDataPage(pageInfo, acApp);
        pageResponseVo.setRows(dataPage.getList());
        pageResponseVo.setTotal((int) dataPage.getTotal());
        jr.setData(pageResponseVo);
        jr.setSuccess(true);
        return jr;
    }

    @RequestMapping("/menuTest")
    @ResponseBody
    public Object menuTest() {
        JsonResult jr = new JsonResult();
        jr.setData("success");
        jr.setSuccess(true);
        return jr;
    }

    /**
     * 获取数据列表
     *
     * @param acApp
     * @return JsonResult
     */
    @RequestMapping("/getDataList")
    @ResponseBody
    public Object getDataList(AcApp acApp) {
        JsonResult jr = new JsonResult();
        jr.setData(applicationCodeService.getDataList(acApp));
        jr.setSuccess(true);
        return jr;
    }


    /**
     * 获取数据
     *
     * @param id
     * @return JsonResult
     */
    @RequestMapping("/getOne")
    @ResponseBody
    public Object getOne(String id) {
        JsonResult jr = new JsonResult();
        jr.setSuccess(true);
        jr.setData(applicationCodeService.selectByPrimaryKey(id));
        return jr;
    }

    /**
     * 获取数据
     *
     * @param appCode
     * @return JsonResult
     */
    @RequestMapping("/getOneByAppCode")
    @ResponseBody
    public Object getOneByAppCode(String appCode) {
        JsonResult jr = new JsonResult();
        jr.setSuccess(true);
        jr.setData(applicationCodeService.selectByAppCode(appCode));
        return jr;
    }

    /**
     * 添加
     *
     * @param acApp
     * @return JsonResult
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody AcApp acApp) {
        JsonResult jr = new JsonResult();
        jr.setSuccess(applicationCodeService.insert(acApp) > 0 ? true : false);
        return jr;
    }

    /**
     * 添加
     *
     * @param acApp
     * @return JsonResult
     */
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody AcApp acApp) {
        JsonResult jr = new JsonResult();
        jr.setSuccess(applicationCodeService.update(acApp) > 0 ? true : false);
        return jr;
    }

    /**
     * 删除
     *
     * @param id
     * @return JsonResult
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        JsonResult jr = new JsonResult();
        jr.setSuccess(applicationCodeService.delete(id) > 0 ? true : false);
        jr.setMsg("工程服务信息修改成功");
        return jr;
    }


    @RequestMapping("/getAppReqCounts")
    @ResponseBody
    public Object getAppReqCounts(String stm, String etm, String servicecode, String guid, String appCode, String servicename) {
//        stm = "2024-02-11 00:00:00";
//        etm = "2024-07-05 00:00:00";
        Timestamp start = Timestamp.valueOf(stm);
        Timestamp end = Timestamp.valueOf(etm);
        JsonResult jr = new JsonResult();
        PageResponseVo pageResponseVo = new PageResponseVo();
        List<SysReqLog> alllist = sysReqLogService.getAppReqAll(guid, appCode, servicename);

        List<SysReqLog> list = sysReqLogService.getAppReqCounts(servicecode, start, end, appCode, servicename);


        List<SysReqLog> zerolist = alllist.stream().filter(u -> list.stream().filter(i -> i.getAppCode().equals(u.getAppCode())).collect(Collectors.toList()).size() == 0).collect(Collectors.toList());

        if (zerolist != null && zerolist.size() > 0) {
            zerolist.forEach(item -> {
                list.add(item);
            });
        }
        alllist.forEach(item -> {
            list.forEach(it -> {
                if (item.getAppCode().equals(it.getAppCode())) {
                    it.setFlag(true);
                }
            });
        });

        jr.setData(list);
        jr.setSuccess(true);
        return jr;
    }

    @RequestMapping("/getAppReqCountsTm")
    @ResponseBody
    public Object getAppReqCountsTm(String guid, String servicename, String servicecode, String appCode, String stm, String etm) {
//        stm = "2024-01-11 00:00:00";
//        etm = "2024-07-05 00:00:00";
        Timestamp start = Timestamp.valueOf(stm);
        Timestamp end = Timestamp.valueOf(etm);

        JsonResult jr = new JsonResult();
        PageResponseVo pageResponseVo = new PageResponseVo();
        List<SysReqLog> list = sysReqLogService.getAppReqCountsTm(servicecode, appCode, start, end);
        DateFormat format3 = new SimpleDateFormat("yyyy-MM-dd");

        list.forEach(i -> i.setReqDateStr(format3.format(i.getReqDate())));

        jr.setData(list);
        jr.setSuccess(true);
        return jr;
    }


}
